﻿#region Copyright
// Copyright (c) 2010, Kazi Manzur Rashid <kazimanzurrashid@gmail.com>.
// This source is subject to the Microsoft Public License. 
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL. 
// All other rights reserved.
#endregion

namespace LiveWriter.Plugins
{
    using System;
    using System.Text;

    internal static class StringExtensions
    {
        // Converted from http://wlwsyntaxhighlighter.codeplex.com
        public static string FixIndentation(this string instance, int tabSize)
        {
            if (string.IsNullOrEmpty(instance))
            {
                return string.Empty;
            }

            string[] lines = instance.Replace(Environment.NewLine, "\n")
                                     .Split(new[] { '\n' });

            int minCount = int.MaxValue;

            foreach (string line in lines)
            {
                if (line.Trim().Length == 0)
                {
                    continue;
                }

                int offset = 0;
                int count = 0;

                for (int i = 0; i < line.Length; i++)
                {
                    if (line[i] == ' ')
                    {
                        count += 1;
                    }
                    else if (line[i] == '\t')
                    {
                        int spaces = tabSize - (i + offset) % tabSize;
                        count += spaces;
                        offset += spaces - 1;
                    }
                    else
                    {
                        break;
                    }
                }

                if (minCount > count)
                {
                    minCount = count;
                }

                if (minCount == 0)
                {
                    return instance;
                }
            }

            StringBuilder indented = new StringBuilder();

            foreach (string line in lines)
            {
                if (line.Trim().Length == 0)
                {
                    indented.Append(Environment.NewLine);
                }
                else
                {
                    StringBuilder newLine = new StringBuilder();
                    int offset = 0;

                    for (int i = 0; i < line.Length; i++)
                    {
                        if (line[i] == ' ')
                        {
                            newLine.Append(" ");
                        }
                        else if (line[i] == '\t')
                        {
                            int spaces = tabSize - (i + offset) % tabSize;
                            newLine.Append(' ', spaces);
                            offset += spaces - 1;
                        }
                        else
                        {
                            newLine.Append(line.Substring(i).TrimEnd());
                            break;
                        }
                    }

                    indented.Append(newLine.ToString().Substring(minCount) + Environment.NewLine);
                }
            }

            return indented.ToString().TrimEnd();
        }

        public static string FormattedCode(this string instance)
        {
            return string.IsNullOrEmpty(instance) ? string.Empty : instance.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;");
        }
    }
}